package com.shuai.jit.controller;


import com.shuai.jit.entity.R;
import com.shuai.jit.entity.course.Article;
import com.shuai.jit.service.impl.ArticleServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api("文章控制类")
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    ArticleServiceImpl articleService;

    @ApiOperation("添加文章")
    @RequestMapping("/addArticle")
    public R add(@RequestBody  Article article){
       Integer integer= articleService.add(article);
       if(integer>0){
           return R.ok().data("article","添加文章成功");
       }else {
           return R.error().data("article","添加文章失败");
       }
    }

    @ApiOperation("更新文章")
    @RequestMapping("/updateArticle")
    public R update(@RequestBody Article article) {
        Integer result = articleService.update(article);
        if(result>0){
            return R.ok().data("article","修改文章成功");
        }else {
            return R.error().data("article","添加文章失败");
        }
    }

    /**
     * 获取所有文章
     * @return
     */
    @ApiOperation("获取所有文章")
    @RequestMapping("/selectAllArticle")
    public R selectAll(Integer current){
        return R.ok().data("article",articleService.selectAll(current));
    }


    /**
     * 根据用户id ，推荐文章，推荐8篇文章
     * 使用协同过滤算法
     * @param userId
     * @return
     */
    @ApiOperation("根据文章来推荐文章")
    @RequestMapping("/selectAllArticleById")
    public R selectAllById(String userId){
        return R.ok().data("article",articleService.getArticle(userId));
    }



    @ApiOperation("根据文章id,来获取文章信息")
    @RequestMapping("/selectById")
    public R selectById(String id,String userId){
        return R.ok().data("article",articleService.selectById(id,userId));
    }




    @ApiOperation("根据用户id，获取该用户所有的文章信息")
    @RequestMapping("/selectByUserId")
    public R selectByUserId(String userId){
        return R.ok().data("article",articleService.selectByUserId(userId));
    }


    @ApiOperation("判断该文章是否已经点过赞")
    @RequestMapping("/isGood")
    public R isGood(String articleId,String userId){
        return R.ok().data("article",articleService.isGood(userId,articleId));
    }


    @RequestMapping("/deleteArticle")
    public R deleteArticle(String id){
        Integer result=articleService.delete(id);
        if(result>0){
            return R.ok().data("article","删除文章成功");
        }else {
            return R.error().data("article","删除文章失败");
        }
    }

    @RequestMapping("/goodArticle")
    public R good(String userId,String articleId){
        Integer result=articleService.good(userId,articleId);
        if(result>0){
            return R.ok().data("article","文章点赞或取消成功");
        }else if(result==-2) {
            return R.error().data("article","请输入id");

        }else {
            return R.error().data("article","文章点赞或取消失败");
        }

    }

}
